package com.aoxun.modular.dictionaryConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aoxun.common.persistence.model.DictionaryConfig;
import com.aoxun.core.util.BeanUtils;
import com.aoxun.modular.dictionaryConfig.service.IDictionaryConfigService;

public class DictConfig {

	
	private static DictConfig INSTANCE;
	
	private static Map<String,String> configMap;
	
	DictConfig(){
		//查询左右配置
		IDictionaryConfigService iDictionaryConfigService = BeanUtils.getBean("dictionaryConfigServiceImpl");
		List<DictionaryConfig> list = iDictionaryConfigService.selectAll();
		configMap = new HashMap();
		toMap(list);
	}
	
	/**
	 * 单例模式,只初始化一次
	 * @return
	 */
	private static DictConfig Instance(){
        if (INSTANCE == null) {
            synchronized (DictConfig.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DictConfig();
                }
            }
        }
        return INSTANCE;
	}
	
	public static String getKey(String key){
		if(configMap==null){
			refresh();
		}
		return configMap.get(key);
	}
	
	public static void refresh(){
		INSTANCE = null;
		Instance();
	}
	
	private void toMap(List<DictionaryConfig> list){
		if(list == null || list.size() == 0){
			return;
		}
		for(DictionaryConfig dict:list){
			configMap.put(dict.getKey(),dict.getValue());
		}
	}
}
